'use strict';

const Controller = require('egg').Controller;

class SeatController extends Controller {

    // 选择楼层
    async seatList() {
        // 楼层id
        console.log(this.ctx.query.id);
        const id = this.ctx.query.id;
        // 是否是今天
        console.log(this.ctx.query.today);
        const today = this.ctx.query.today;
        try {
            let list = '';
            if (today) {
                // 是今天
                list = await this.ctx.service.seat.seatList("floor", id);
            } else {
                // 不是今天
                list = await this.ctx.service.seat.seatList("mfloor", id);
            }
            if (!list.result) {
                this.ctx.body = {
                    code: -1,
                    data: list,
                    msg: '查询失败'
                }
            } else {
                this.ctx.body = {
                    code: 200,
                    data: list,
                    msg: '查询成功'
                }
            }

        } catch (error) {
            this.ctx.body = {
                code: -1,
                data: list,
                msg: '查询失败'
            }
        }
    };

    // 预约座位
    async selectSeat() {
        // 传递参数
        console.log("传递参数: ", this.ctx.request.body);
        const datas = this.ctx.request.body
        try {
            const data = await this.ctx.service.seat.selectSeat("seat_order", datas.uid, datas.fid, datas.sid, datas.today);
            this.ctx.body = {
                code: 200,
                data: data,
                msg: "预约成功"
            }
        } catch (error) {
            this.ctx.body = {
                code: -1,
                data: error,
                msg: "预约失败"
            }
        }
    }

    /* 查看当前预约座位 */
    async seatOrderList() {
        console.log(this.ctx.query.uid);
        try {
            const list = await  this.ctx.service.seat.seatOrderList("seat_order", this.ctx.query.uid);
            console.log(list);
            if (list.result) {
                this.ctx.body = {
                    code: 200,
                    data: list,
                    msg: "查询成功"
                }
            }else {
                this.ctx.body = {
                    code: -1,
                    data: list,
                    msg: "当前没有预约位置"
                }
            }

        } catch (error) {
            this.ctx.body = {
                code: -1,
                data: error,
                msg: "查询失败"
            }
        }

    }

    /* 切换座位 */
    async change() {
        // 传递参数
        console.log("传递参数: ", this.ctx.request.body);
        const datas = this.ctx.request.body
        try {
            const data = await this.ctx.service.seat.change("seat_order", datas.id, datas.fid, datas.sid);
            this.ctx.body = {
                code: 200,
                data: data,
                msg: "切换成功"
            }
        } catch (error) {
            this.ctx.body = {
                code: -1,
                data: error,
                msg: "预约失败"
            }
        }
    }

    // 预约座位
    async regular() {
        // 传递参数
        console.log("传递参数: ", this.ctx.request.body);
        const datas = this.ctx.request.body
        try {
            const data = await this.ctx.service.seat.regular("useat", datas.uid, datas.fid, datas.sid);
            this.ctx.body = {
                code: 200,
                data: data,
                msg: "成功设置常用座位"
            }
        } catch (error) {
            this.ctx.body = {
                code: -1,
                data: error,
                msg: "设置常用座位失败"
            }
        }
    }


}



module.exports = SeatController;

